/**
 * Created by jocky on 07/10/2017.
 */
$().ready(function () {
    //出金历史
    MoneyObj.history();
});


var MoneyObj = {
    /**
     * 提交
     */
    submit: function () {
        var form = $("#form_moneyWithdraw");
        var params = {};
        var validFlag = true;
        $.each(form.find("input,select"), function (i, v) {
            if (v.name != "") {
                var val = $.trim(v.value);
                if (!val || val == '0') {
                    $(v).focus();
                    setNotify($(v).attr('data-error'), true);

                    validFlag = false;
                    return false;
                }
                params[v.name] = val;
            }
        });

        if (!validFlag) {
            return false;
        }

        $.post('/money/withdraw-submit', params, function (json) {
            var obj = {
                title: '提示',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: json.code == 1 ? 'info' : 'error'
            };
            new PNotify(obj);

            if (json.code == 1) {
                form[0].reset();
                MoneyObj.history(1, 1);
            }
        }, 'json');

    },

    /**
     * 出金历史
    //  */
    history: function (type, direction) {
        //显示loading
        show_loading_table('moneyWithdrawHistory_table', 4);

        var params = {};

        var pageNo_dom = 'moneyWithdrawHistory_pageNo';
        var pageTotal_dom = 'moneyWithdrawHistory_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        var formObj = $('#form_moneyWithdrawHistory');
        $.each(formObj.find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });
        $.getJSON('/money/withdraw-history', params, function (json) {
            var t = json;
            $("#" + pageNo_dom).val(parseInt(t.pageNo));
            $("#" + pageTotal_dom).html(parseInt(t.pageTotal));
            var sumMoney = parseFloat(t.sumMoney).toFixed(2);

            var tr_head = "<tr>" +
                "<td><i style='color: red;'>汇总</i></td>" +
                "<td></td>" +
                "<td class='text-right'><i>" + sumMoney + "</i></td>" +
                "<td></td>" +
                "</tr>";

            var tr = "";
            $.each(t.lists, function (i, v) {
                var status;
                var operate = " / ";
                switch (parseInt(v.status)) {
                    case 0:
                        status = "<label class='label label-warning'>未处理</label>";
                        operate = "<a href='javascript:void(0)' onclick='MoneyObj.cancel(" + v.id + ")'>取消</a>";
                        break;
                    case 1:
                        status = "<label class='label label-default'>请求中</label>";
                        break;
                    case 2:
                        status = "<label class='label label-danger'>失败</label>";
                        break;
                    case 3:
                    case 6:
                        status = "<label class='label label-success'>成功</label>";
                        break;
                    case 4:
                        status = "<label class='label label-default'>平台取消</label>";
                        break;
                    case 5:
                        status = "<label class='label label-default'>用户取消</label>";
                        break;
                    default:
                        status = "";
                        operate = " / ";
                        break;
                }
                tr += "<tr>";
                tr += "<td>" + convertDate(v.withdraw_open_time, 1) + "</td>";
                tr += "<td>" + status + "</td>";
                tr += "<td class='text-right'><i>" + parseFloat(v.money) + "</i></td>";
                tr += "<td class='text-center'>" + operate + "</td>";
                tr += "</tr>";
            });
            $("#moneyWithdrawHistory_table").find("tbody").html(tr_head + tr);
            page_content_onresize();
        })
    },

    // /**
    //  * 取消
    //  * @param id
    //  */
    cancel: function (id) {
        swal({
            title: '确定取消该笔出金吗？',
            text: " ",
            type: 'warning',
            showCancelButton: true,
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonColor: '#3085d6',
            confirmButtonText: '确定'
        }).then(function () {
            $.getJSON('/money/withdraw-cancel', {id: id}, function (json) {
                var obj = {
                    title: '提示',
                    text: json.msg,
                    styling: 'bootstrap3',
                    delay: 5000,
                    type: json.code == 1 ? 'info' : 'error'
                };
                new PNotify(obj);

                MoneyObj.history(3, 0);
            })
        }).catch(swal.noop);


    }
};